package com.runmi.webchat.aftersale.common.mail;

import java.io.IOException;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.security.auth.message.MessageInfo;

import org.springframework.stereotype.Component;

public class SendMessage {

	//邮件通信会话
		static Session session;
		
		static String smtp = "smtp.retailyoupin.com"; // 设置发送邮件所用到的smtp   
		static  String servername = "global.yp@retailyoupin.com";   
		static  String serverpaswd = "Abc@1234";   
		
		
		public static void main(String[] args) throws Exception {
			List<String> toAddrs=new ArrayList<String>();
			toAddrs.add("107639524@qq.com");
			sslSend(toAddrs,"test","test");
		 }
		
		
		public static boolean sslSend(List<String> toAddrs,String subject,String content) 
	            throws AddressException, MessagingException, IOException{
	          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	          final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	          // Get a Properties object
	          Properties props = new Properties();
	          props.setProperty("mail.smtp.host",smtp);
	          props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
	          props.setProperty("mail.smtp.socketFactory.fallback", "false");
	          props.setProperty("mail.smtp.port", "465");
	          props.setProperty("mail.smtp.socketFactory.port", "465");
	          props.put("mail.smtp.auth", "true");
	          try{
	        	  final String username = servername;
		          final String password = serverpaswd;
		          Session session = Session.getDefaultInstance(props, new Authenticator(){
		              protected PasswordAuthentication getPasswordAuthentication() {
		                  return new PasswordAuthentication(username, password);
		              }});
		          Message msg = new MimeMessage(session);
		         
		          // 设置发件人和收件人
		          msg.setFrom(new InternetAddress(servername));
		          List<String> tos = toAddrs;
		          Address to[] = new InternetAddress[tos.size()];
		          for(int i=0;i<tos.size();i++){
		              to[i] = new InternetAddress(tos.get(i));
		          }
		          // 多个收件人地址
		          msg.setRecipients(Message.RecipientType.TO, to);
		          msg.setSubject(subject); // 标题
		          msg.setText(content);// 内容
		          msg.setSentDate(new Date());
		          Transport.send(msg);
		          System.out.println("EmailUtil ssl协议邮件发送打印" +msg.toString());
		          return true;
	          }catch(Exception ex){
	        	  ex.printStackTrace();
	          }
	          return false;
	         	          
	         }
		
		public static boolean sslHtmlSend(List<String> toAddrs,String subject,String html,List<String> ccMail) 
	            throws AddressException, MessagingException, IOException{
	          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	          final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	          // Get a Properties object
	          Properties props = new Properties();
	          props.setProperty("mail.smtp.host",smtp);
	          props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
	          props.setProperty("mail.smtp.socketFactory.fallback", "false");
	          props.setProperty("mail.smtp.port", "465");
	          props.setProperty("mail.smtp.socketFactory.port", "465");
	          props.put("mail.smtp.auth", "true");
	          try{
	        	  final String username = servername;
		          final String password = serverpaswd;
		          Session session = Session.getDefaultInstance(props, new Authenticator(){
		              protected PasswordAuthentication getPasswordAuthentication() {
		                  return new PasswordAuthentication(username, password);
		              }});
		          Message msg = new MimeMessage(session);
		         
		          // 设置发件人和收件人
		          msg.setFrom(new InternetAddress(servername));
		          List<String> tos = toAddrs;
		          Address to[] = new InternetAddress[tos.size()];
		          for(int i=0;i<tos.size();i++){
		              to[i] = new InternetAddress(tos.get(i));
		          }
		          
		          if(null!=ccMail && ccMail.size()>0){
		        	  Address[] ccAdresses = new InternetAddress[ccMail.size()];
		        	  for(int i=0;i<ccMail.size();i++){
		        		  ccAdresses[i] = new InternetAddress(ccMail.get(i));
		        	  }		        	
		        	 // ccAdresses[1]=new InternetAddress("gongmf@retailyoupin.com");	               
		        	  msg.setRecipients(Message.RecipientType.CC, ccAdresses);
		          }
		          
		          // 多个收件人地址
		          msg.setRecipients(Message.RecipientType.TO, to);
		          msg.setSubject(subject); // 标题
		          msg.setContent(html, "text/html;charset=utf-8");
		          msg.setSentDate(new Date());
		          
		          Transport.send(msg);
		          System.out.println("EmailUtil ssl协议邮件发送打印" +msg.toString());
		          return true;
	          }catch(Exception ex){
	        	  ex.printStackTrace();
	          }
	          return false;
	         	          
	         }
		
		/**
		 * 邮件配置参数和连接接收邮件服务器
		 * @throws MessagingException
		 */
		 private static void init(){
			if(null==session){
					Properties properties=new Properties();
				
			        javax.mail.internet.MimeMessage mimeMsg; // MIME邮件对象   
			  
			        properties = java.lang.System.getProperties(); // 获得系统属性对象   
			        properties.put("mail.smtp.host", smtp); // 设置SMTP主机   
			        properties.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证   
			        // 到服务器验证发送的用户名和密码是否正确   
			        EmailAutherticatorbean myEmailAuther = new EmailAutherticatorbean(   
			                servername, serverpaswd);   
			        // 设置邮件会话   
			        session = javax.mail.Session.getInstance(properties, (Authenticator) myEmailAuther);   
			           
			        //设置debug打印信息   
			        session.setDebug(true);   

			}
			
		}
		
		public static boolean sendEmail(List<String> toAddrs,String subject,String content) throws AddressException, MessagingException{
			boolean result=false;
			try{	
				init();
				//建立一封邮件
				MimeMessage message=new MimeMessage(session);
				//设置发送者和接收者
				message.setFrom(new InternetAddress(servername));
				InternetAddress[] sendTo = new InternetAddress[toAddrs.size()];   
				for (int i = 0; i < toAddrs.size(); i++) {      
		            sendTo[i] = new InternetAddress(toAddrs.get(i));   
		        }   

				message.setRecipients(Message.RecipientType.TO,sendTo);
				//设置主题
				message.setSubject(subject);
				//设置日期
				message.setSentDate(new Date(System.currentTimeMillis()));
				//设置正文
				message.setText(content);
				Transport  transport = session.getTransport("smtp");
				transport.send(message);
				result=true;
			}catch(Exception ex){
				ex.printStackTrace();
			}
			
			return result;
		
		}

}
